<?php
namespace app\lib\model;

use app\lib\model\Price;
class Tool{
    /**
     * 求经纬度乘积
     * [getProduct description]
     * @param  [type] $longitude [description]
     * @param  [type] $latitude  [description]
     * @return [type]            [description]
     */
    public static function getProduct($longitude,$latitude){
        return abs(Price::pricecalc(Price::pricecalc($longitude,'*',$latitude,15),'*',100000000000000,0));
    }


    /**
     * 计算两点之间的距离
     * @param $lng1 经度1
     * @param $lat1 纬度1
     * @param $lng2 经度2
     * @param $lat2 纬度2
     * @param int $unit 1=>m，2=>km
     * @param int $decimal 位数
     * @return float
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2)
    {

        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI           = 3.1415926535898;

        $radLat1 = $lat1 * $PI / 180.0;
        $radLat2 = $lat2 * $PI / 180.0;

        $radLng1 = $lng1 * $PI / 180.0;
        $radLng2 = $lng2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;
        if ($unit === 2) {
            $distance /= 1000;
        }

        return round($distance, $decimal);
    }
    /**
     * 计算  2*x+3*y=100    x,y 不能是负数 共有几种
     * [equation description]
     * @param  int|integer $a   [description]
     * @param  int|integer $b   [description]
     * @param  int|integer $tot [description]
     * @return [array]           [description]   array(2) {
                      [0] => array(2) {
                        [0] => int(2)
                        [1] => int(32)
                      }
                      [1] => array(2) {
                        [0] => int(5)
                        [1] => int(30)
                      }
     */
    public static function equation( int $a = 2, int $b = 3,int $tot = 100){
        $arr = [];
        for($i=0;$i<=$tot;$i++){
            for($j=0;$j<=$tot;$j++){
                if($a*$i+$b*$j == $tot){
                    $arr[] = [$i,$j];
                }
            }
        }
        return $arr;
    }
    /**
     * 腾讯地图地址解析  地址转坐标
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
     * [addressResolution description]
     * @param  [type] $address [description]
     * @return [type]          [description]
     */
    public static function addressResolution($address)
    {
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?address='.$address.'&key='.config('api.wx_map_key');
        $res = curl_send($url,[],[],false);
        if($res['status'] == 0) return $res['result']['location'];
        return false;
    }
    /**
     * 腾讯地图坐标转地址
     * https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
     * [coordinateToAddress description]
     * @param  [type] $latitude  [description]
     * @param  [type] $longitude [description]
     * @return [type]            [description]
     */
    public static function coordinateToAddress($latitude,$longitude)
    {
        $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location='.$latitude.','.$longitude.'&key='.config('api.wx_map_key');
        $res = curl_send($url,[],[],false);
        if($res['status'] == 0) return $res['result'];
        return false;
    }
    /**
     * 根据身份证号获取年龄
     * [getAgeByID description]
     * @param  [type] $id_card [description]
     * @return [type]          [description]
     */
    public static function getAgeByID($id_card)
    {
        if(!$id_card) return '';
        $date = strtotime(substr($id_card,6,8));
        $today = strtotime('today');
        $diff = floor(($today-$date)/86400/365);
        $age = strtotime(substr($id_card, 6,8).' +'.$diff.'years') > $today ? $diff : ($diff+1);
        return $age;
    }
    /**
     * 根据身份证号码获取性别  1男 2女
     * [getSex description]
     * @param  [type] $id_card [description]
     * @return [type]          [description]
     */
    public static function getSex($id_card)
    {
        $postion = strlen($id_card == 15) ? -1 : -2;
        if(substr($id_card,$postion,1) % 2 == 0) return 2;
        return 1;
    }
}
?>